// pages/index/index.js
const config = require('../../config')
const util = require('../../utils/util')
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name:'神秘用户',
        slogan:'万水千山总是情，发个祝福行不行',
        filePath: 'http://duodianzhufu-1253586992.cosgz.myqcloud.com/Resources/'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            filePath: config.filePath +'Resources/'
        })
        //this.getuserinfo()
        this.getrule()
    },
    //获取首页信息
    getrule: function () {
        var that = this
        
        if (app.globalData.uid && app.globalData.nickName) {
            var headerkey = util.createheaderkey()
            wx.request({
                url: config.host + '/api/homepage/homepage',
                method: 'GET',
                header: {
                    'content-type': 'application/json',
                    'key': headerkey.key,
                    'time': headerkey.timestamp
                },
                data: {
                    uid: app.globalData.uid,
                    ruleid: 1
                },
                success: function (res) {
                    if (res.data.code == 200) {
                        var data = res.data.data

                        that.setData({
                            rule: data.rule,
                            name: data.username.name
                        })
                    }else{
                        wx.showModal({
                            title: '失败',
                            content: res.data.msg,
                            showCancel: false
                        })
                    }
                },
                fail: function (res) {
                    wx.showToast({
                        title: '规则获取失败!',
                        duration: 3000
                    })
                }
            })
        } else {
            setTimeout(function () {
                that.getrule()
            }, 1000)
        }
    },
    //保存formid
    saveform:function(e){
        var formid = e.detail.formId
        app.saveformid(formid)
    },
    
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
    
    },

    /**
     * 跳转到发祝福页面
     */
    gotosendblessings: function (e) {
        var types = e.currentTarget.dataset.types
        //console.log(id)
        wx.navigateTo({
            url: '/pages/index/sendblessings?types=' + types
        })
    }
})